package com.zxl.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModelProperty;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zxw
 * @date 2023/3/31
 */
public class Page<T> {
    @ApiModelProperty("当前页")
    private int pageNumber;
    @ApiModelProperty("页大小")
    private int pageSize;
    @ApiModelProperty("总记录数")
    private long totalCount;
    @ApiModelProperty("总页数")
    private int totalPage;
    @ApiModelProperty("数据")
    private List<T> data;


    private Page() {
    }

    public Page(int pageNumber, int pageSize, List<T> data, long totalCount) {
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
        this.data = data;
        this.totalCount = totalCount;
    }

    public Page(int pageNumber, int pageSize) {
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
        this.data = new ArrayList<>();
        this.totalCount = 0L;
    }

    public static Page emptyPage(int pageNumber, int pageSize){
        return new Page<>(pageNumber,pageSize,new ArrayList<>(),0L);
    }

    public Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page page){
        this.pageNumber = Integer.valueOf(page.getCurrent()+"");
        this.pageSize = Integer.valueOf(page.getSize()+"");
        this.data = page.getRecords();
        this.totalCount = page.getTotal();
        if(totalCount!=0){
            long l = totalCount % pageSize;
            long totalPage = totalCount / pageSize;
            if(l>0){
                totalPage++;
            }
            this.totalPage = Integer.valueOf(totalPage+"");
        }
    }

    public Page(PageInfo pageInfo){
        this.pageNumber = pageInfo.getPageNum();
        this.pageSize = pageInfo.getPageSize();
        this.totalCount = pageInfo.getTotal();
        this.totalPage = pageInfo.getPages();
        this.data = pageInfo.getList();
    }

    public Page(IPage iPage){
        this.pageNumber = Integer.valueOf(""+iPage.getCurrent());
        this.pageSize = Integer.valueOf(""+iPage.getSize());
        this.totalCount = iPage.getTotal();
        this.data = iPage.getRecords();
    }

    public int getPageCount(){
        if(totalCount!=0){
            long l = totalCount % pageSize;
            long totalPage = totalCount / pageSize;
            if(l>0){
                totalPage++;
            }
            return Integer.valueOf(totalPage+"");
        }else{
            return 0;
        }
    }


    public static Page pageInfoConvert(PageInfo pageInfo){
        return new Page(pageInfo);
    }

    public static Page Convert(IPage iPage){
        return new Page(iPage);
    }
    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
}
